/**
 * Created by jianglinjie on Mon Oct 09 2017
 * @flow
 */
'use strict';

import type { RegisterResult, RegisterParam } from '../../serviceTypes';
import * as RegisterType from '../../constants/RegisterType';
import { FetchClient, JSONRequest } from '../../network';
import ConfigService from '../config';

const REGISTER_PATH = '/users';

class RegisterService {
  async registerWithUserName(userName: string, pwd: string): Promise<RegisterResult> {
    let param = {
      userName: userName,
      pwd: pwd
    };

    let userInfo = await this.register(RegisterType.USERNAME_PWD, param);
    return userInfo;
  }

  async registerWithPhone(phone: string, pwd: string, captcha: string): Promise<RegisterResult> {
    let param = {
      phone: phone,
      pwd: pwd,
      captcha: captcha
    };

    let userInfo = await this.register(RegisterType.PHONE_PWD_CAPTCHA, param);
    return userInfo;
  }

  async register(type: string, param: RegisterParam): Promise<RegisterResult> {
    let request = new JSONRequest(
      ConfigService.apiBaseHost + REGISTER_PATH, 
      { type: type }, 
      param
    );

    let body = await FetchClient.start(request);
    
    return body.data;
  }
}

export default new RegisterService();